<?
class AddTouristAttractionForm extends Form
{
    private $tuas = array();
    function AddTouristAttractionForm()
    {
        Form::Form("AddTouristAttractionForm");
        $this->link_js("javascript/jquery/jquery.ui.core.js");
        $this->link_js("javascript/jquery/jquery.ui.widget.js");
        $this->link_js("javascript/jquery/jquery.ui.mouse.js");
        $this->link_js("javascript/jquery/jquery.ui.sortable.js");
        $this->link_js("javascript/tiny_mce/jquery.tinymce.js");
        $this->link_js("javascript/tiny_mce/tiny_mce.js");
        
        
        //$this->link_css('style/jquery-ui-1.9.2.custom.css');
        $this->link_css('style/msgBoxLight.css');
        //$this->link_js('javascript/jquery/jquery-ui-1.9.2.custom.min.js');
        $this->link_js("javascript/jquery/jquery.msgBox.js");
        $this->link_js("javascript/hotelform.js");

        $this->link_outside_js("http://maps.google.com/maps/api/js?sensor=false");
        $this->link_js("javascript/map/geocoder_tool.js");
        $this->link_css("style/map/geocoder_tool.css");
        if(Url::get('cmd')=='edit'){
                        CGlobal::$website_title = "Sửa tin";
			$id = (int)Url::get('id');
			if($id){
				$this->tuas=DB::select('tourist_attractions','id='.$id);
			}
			
			if(!$this->tuas)
			Url::redirect_current();
		}
                else{
                    CGlobal::$website_title = "Thêm tin tức";
			$this->tuas=array(
                                            'name' => '',
                                            'user_c' => '',
                                            'user_m' => '',
                                            'user_del' => '',
                                            'user_c_email' => '',
                                            'user_m_email' => '',
                                            'user_del_email' => '',
                                            'time_c' => '',
                                            'time_m' => '',
                                            'time_del' => '',
                                            'status' => '',
                                            'alias' => '',
                                            'intro' => '',
                                            'content' => '',
                                            'keyword' => '',
                                            'web_tittle' => '',
                                            'city_id' => '',
                                            'img_url' => '',
                                            'img_server' => '',
                                            'latitude' => '',
                                            'longitude' => '',
                                            'avatar' => '',
                                            'cate_id' => '',
                                                            );
                }
    }
    function on_submit()
    {
        $tuas['name']                 = ucfirst(Url::getParam('tuas_name', ''));
        $tuas['alias'] = strtolower(String::_name_cleaner(Unicode::stripUnicode($tuas['name'] ),'-'));
        $tuas['city_id']                = Url::getParam('tuas_city_id', '');
        $tuas['intro']                  = ucfirst(Url::getParam('tuas_intro',''));
        $tuas['content'] 			= Url::get('tuas_content','');
        $tuas['status'] 		= Url::getParamInt('tuas_status', '');
        $tuas['keyword'] 		= Url::getParam('tuas_key', '');
        $tuas['web_tittle']             = Url::getParam('tuas_name', '');
        $tuas['latitude'] 			= Url::get('hotel_latitude','');
        $tuas['longitude'] 			= Url::get('hotel_longitude','');
        $valid = true;
        $id = Url::getParamInt('id');
        if($tuas['name'] =='')
        {
            $valid = false;
            $this->setFormError("error_name","Tiêu đề tin không được để trống!");
        }
        else{
                if($tuas['intro'] =='')
                {
                    $valid = false;
                    $this->setFormError("error_name","Mô tả không được để trống!");
                }  else {                    
                        if($tuas['content'] =='')
                        {
                            $valid = false;
                            $this->setFormError("error_name","Nội dung không được để trống!");
                        }  else {
                                if($tuas['status'] =='')
                                {
                                    $valid = false;
                                    $this->setFormError("error_name","Trạng thái không được để trống!");
                                }
                        }
                }
        }

        if($valid)
        {
            if(isset($_FILES["file_up"])){
                $avatar = $_FILES["file_up"]["name"];
                    if($_FILES["file_up"]["tmp_name"]){
                        if(Upload::storage_check_dir("tuarist/", true, IMAGE_SERVER_NO))
                        {
                            $img_ext = explode(".", $avatar);
                            $file_ext = array_pop($img_ext);
                            if(!in_array($file_ext,array('jpg','png','jpeg','gif'))){
                                $this->setFormError("error_file_ext","File tải lên không đúng định dạng!");
                                return ;
                            }
                            $file_name = UP_FOLDER."/tuarist/" . String::_name_cleaner(Unicode::stripUnicode($tuas["name"]),'-').'.'.$file_ext ;
                            $sourceName = $_FILES["file_up"]["tmp_name"];
                            if(!Upload::storage_put_file($file_name, $sourceName, IMAGE_SERVER_NO, FTP_BINARY,1)){
                                $this->setFormError("error_upload_file","Có lỗi xảy ra khi upload file!");
                                return ;
                            };
                            $tuas['img_url'] = $file_name;
                            $tuas['img_server'] = IMAGE_SERVER_NO;
                        }
                    }
                }
            //insert
            if($id == ''){
                $tuas['user_c'] 		= User::id();
                $tuas['time_c']                 = TIME_NOW;
                $tuas['user_c_email']           = User::user_email();
                $tuas['user_m']                 = User::id();
                $tuas['user_m_email']           = User::user_email();
                $tuas['time_m']                 = TIME_NOW;
                DB::insert('tourist_attractions',$tuas);
                $this->setFormSucces("success","Save hành công!");
            }else{
                //edit
                $tuas['user_m']                 = User::id();
                $tuas['user_m_email']           = User::user_email();
                $tuas['time_m']                 = TIME_NOW;
                DB::update_id('tourist_attractions', $tuas, $id);
                $this->setFormSucces("success","Save hành công!");
            }
        }
    }

    function draw()
    {
        global $display;
        $this->beginForm(true);
        if(isset($_GET['cmd'])){
            $display->add("tuas_name",Url::getParam('tuas_name',  $this->tuas['name']));
            $display->add("tuas_latitude",Url::getParam('hotel_latitude',  $this->tuas['latitude']));
            $display->add("tuas_longitude",Url::getParam('hotel_longitude',  $this->tuas['longitude']));
            $display->add("tuas_intro",Url::getParam('tuas_intro',$this->tuas['intro']));
            $display->add("tuas_content",Url::getParam('tuas_content', $this->tuas['content']));
            $display->add("tuas_key",Url::getParam('tuas_key', $this->tuas['keyword']));
            $display->add("status",Url::getParamInt('tuas_status', $this->tuas['status']));
            $display->add("img_url",Db_Provider::getImageUrl($this->tuas['img_url']));
            $display->add("avatar",Db_Provider::getImageUrl( $this->tuas['img_url']));
            $news_cate_id = Url::get("news_cate_id",$this->tuas['city_id']);
            $display->add('news_cate_id', $news_cate_id);
        }
        
        $display->add('tuas',$this->tuas);
        $display->add('tuas_status', array(1=>"Hiện",2=>"Ẩn"));
        $display->add('tuas_city_id',  $this->tuas['city_id']); 
        $display->add("title","THÊM TIN MỚI");
        $display->add("title_submit","Thêm mới");
        $location = Db_Location::getLocationByParentId(0);
        $display->add('province',$location);
        $display->add("message",$this->showFormErrorMessages(1));
        $display->add("sucess_msg",$this->showFormSuccesMessages(1));
        
        $display->output("AddTouristAttraction");
        
        $this->endForm();
    }

    
}
?>